﻿using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SuperSocket;
using SuperSocket.Command;
using SuperSocket.ProtoBase;
using SuperSocketV2Sample.Command.Commands;

namespace SuperSocketV2Sample.Command
{
    class Program
    {
        static async Task Main()
        {
            //创建宿主：用Package的类型和PipelineFilter的类型创建SuperSocket宿主。
            var host = SuperSocketHostBuilder.Create<StringPackageInfo, CommandLinePipelineFilter>()
                //注册用于处理连接、关闭的Session处理器
                .UseSessionHandler(async (session) =>
                {
                    Console.WriteLine($"session connected: {session.RemoteEndPoint}");

                    //发送消息给客户端
                    var msg = $@"Welcome to TelnetServer: {session.RemoteEndPoint}";
                    await session.SendAsync(Encoding.UTF8.GetBytes(msg + "\r\n"));
                }, async (session, reason) =>
                {
                    await Task.Delay(0);
                    Console.WriteLine($"session {session.RemoteEndPoint} closed: {reason}");
                })
                .UseCommand((commandOptions) =>
                {
                    //注册命令
                    commandOptions.AddCommand<AddCommand>();
                    commandOptions.AddCommand<DivCommand>();
                    commandOptions.AddCommand<MulCommand>();
                    commandOptions.AddCommand<SubCommand>();
                    commandOptions.AddCommand<EchoCommand>();
                })
                //配置日志
                .ConfigureLogging((hostCtx, loggingBuilder) =>
                {
                    loggingBuilder.AddConsole();
                })
                .Build();
            await host.RunAsync();
        }
    }
}
